// src/utils/storage.js
// Local storage utility functions for storing auth tokens and user data

/**
 * Set JWT token in local storage
 * @param {string} token - JWT token to store
 */
export const setToken = (token) => {
  if (!token) return;
  localStorage.setItem('auth_token', token);
};

/**
 * Get JWT token from local storage
 * @returns {string|null} - JWT token or null if not found
 */
export const getToken = () => {
  return localStorage.getItem('auth_token');
};

/**
 * Remove JWT token from local storage
 */
export const removeToken = () => {
  localStorage.removeItem('auth_token');
};

/**
 * Set user data in local storage
 * @param {Object} userData - User data to store
 */
export const setUserData = (userData) => {
  if (!userData) return;
  localStorage.setItem('user_data', JSON.stringify(userData));
};

/**
 * Get user data from local storage
 * @returns {Object|null} - User data object or null if not found
 */
export const getUserData = () => {
  const userData = localStorage.getItem('user_data');
  return userData ? JSON.parse(userData) : null;
};

/**
 * Remove user data from local storage
 */
export const removeUserData = () => {
  localStorage.removeItem('user_data');
};

/**
 * Clear all auth-related data from local storage
 */
export const clearAuthData = () => {
  removeToken();
  removeUserData();
};

/**
 * Get user ID from local storage
 * @returns {string|null} - User ID or null if not found
 */
export const getUserId = () => {
  const userData = getUserData();
  return userData ? userData.id : null;
};

export const getUserName = () => {
  const userData = getUserData();
  return userData && userData ? userData.name : null;
};

export const getConstellation = () => {
  const userData = getUserData();
  return userData && userData ? userData.constellation : null;
};


export const getIsLock = () => {
  const userData = getUserData();
  return userData && userData ? userData.is_lock : null;
};

export const getVipExpired = () => {
  const userData = getUserData();
  return userData && userData ? userData.vip_expired : null; 
};


export const getIsPerpetualVip = () => {
  const userData = getUserData();
  return userData && userData ? userData.is_perpetual_vip : false;
};


export const getEmail = () => {
  const userData = getUserData();
  return userData && userData ? userData.email : "";
};

/**
 * 检查用户权限状态
 * @returns {boolean} - 返回用户是否有权限使用功能
 */
export const checkUserPermission = () => {
  // 检查是否是永久VIP
  if (getIsPerpetualVip()) {
    return true;
  }
  
  // 检查是否被锁定
  if (!getIsLock()) {
    return false;
  }
  
  // 检查VIP是否过期
  const vipExpired = getVipExpired();
  
  if (vipExpired) {
    const expiredDate = new Date(vipExpired);
    console.log("vipExpired", expiredDate.toISOString());
    
    const currentTime = new Date().getTime();
    if (currentTime > expiredDate.getTime()) {
      clearAuthData(); // 清除token和用户数据
      return false;
    }
  } else {
    return false;
  }
  
  return true;
};

export const getEndpoint = () => {
  return import.meta.env.VITE_API_URL;
};
